/**
 * Created by HUANGFR2 on 8/19/2016.
 */
const mongoose = require('mongoose');
const Stomp = require('stomp-client');
const oc_destination = '/queue/orderComplain.queue';
const mg_destination = '/queue/merchantGrade.queue';
const ma_destination = '/queue/merchantAudit.queue';
const da_destination = '/queue/dishAd.queue';
var Merchant = mongoose.model('Merchant');
var Dish = mongoose.model('Dish');
var Complaint = mongoose.model('Complaint');

var client = new Stomp('zha-ita113-w7', 61613, '', '');

module.exports = client.connect(function (seesionId) {
    console.log('JMS client started!...');
    client.subscribe(oc_destination, function (body, headers) {
        console.log('receive orderComplain.queue : ', body);
        var complaint_ = JSON.parse(body);
        var complaint = new Complaint({
            _id: complaint_.oId,
            c_id: complaint_.cId,
            merchant: complaint_.mId
        });
        complaint.save(function (err) {
            if (err) {
                console.error('order complaint jms save order complaint error : ' + err);
            }
        });
    });
    //client.publish(destination, 'sadasd');

    client.subscribe(mg_destination, function (body, headers) {
        console.log('receive merchantGrade.queue : ' + body);
        var merchant_ = JSON.parse(body);
        Merchant.findOne({_id: merchant_.mId}, function (err, merchant) {
            if (err) {
                console.error(' merchant grade jms find merchant error : ' + err);
            } else {
                if (merchant) {
                    merchant.low_warning = true;
                    merchant.save(function (err) {
                        if (err) {
                            console.error(err)
                        }
                    })
                }
            }
        })
    });

    client.subscribe(ma_destination, function (body, headers) {
        console.log('receive merchantAudit.queue : ' + body);
        var merchant_ = JSON.parse(body);
        var merchant = new Merchant({
            _id: merchant_.mId
        });
        merchant.save(function (err) {
            if (err) {
                console.error('merchant audit jms save merchant error : ' + err);
            }
        })
    });

    client.subscribe(da_destination, function (body, headers) {
        console.log('receive dishAd.queue : ' + body);
        var dish_ = JSON.parse(body);
        //Dish.findOne({_id : dish_.dId}, function(err, dish){
        //    if(err){
        //        console.error('dish audit jms find dish error : ' + err);
        //    } else {
        //        if(dish){
        //            dish.status = 0;
        //            dish.createDate = new Date();
        //        } else {
        //            dish = new Dish({
        //                _id : dish_.dId,
        //                merchant : dish_.mId
        //            });
        //        }
        //        dish.save(function (err) {
        //            if(err){
        //                console.log('dish audit jms save dish error : ' + err);
        //            }
        //        });
        //    }
        //});
        var dish = new Dish({
            _id: dish_.dId,
            merchant: dish_.mId
        });
        dish.save(function (err) {
            if (err) {
                console.log('dish audit jms save dish error : ' + err);
            }
        });
    });
});
//
//client.connect(function (seesionId) {
//    console.log('JMS merchant grade client started!...');
//    client.subscribe(mg_destination, function (body, headers) {
//        console.log('receive merchantGrade.queue : ' + body);
//    })
//});